//
//  XMFriendListView.m
//  XMJJ_zmy
//
//  Created by 丁浩 on 2025/7/7.
//

#import "XMFriendListView.h"
#import "XMFriendListCell.h"

@interface XMFriendListView () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tbl;

@property (nonatomic, strong) NSMutableArray *friendModelArr;

@end

@implementation XMFriendListView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    [self addSubview:self.navView];
    self.navTitleLabel.text = @"关注列表";
    [self addSubview:self.tbl];
}

- (void)setContentWithFriendModelArr:(NSArray *)friendModelArr
                                page:(NSInteger)page {
    [_tbl.mj_header endRefreshing];
    if (page == 1) {
        [self.friendModelArr removeAllObjects];
    }
    if (friendModelArr.count < 10) {
        [_tbl.mj_footer endRefreshingWithNoMoreData];
    }
    else {
        [_tbl.mj_footer endRefreshing];
    }
    [self.friendModelArr addObjectsFromArray:friendModelArr];
    [_tbl reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.friendModelArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return KScaleWidth(77.0);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XMFriendListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XMFriendListCellName" forIndexPath:indexPath];
    [cell setContentWithFriendModel:self.friendModelArr[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    XMFriendModel *friendModel = self.friendModelArr[indexPath.row];
    self.userSelectBlock ? self.userSelectBlock (friendModel.followerId, friendModel.nickname, friendModel.avatar) : nil;
}

#pragma mark - Init
- (UITableView *)tbl {
    if (!_tbl) {
        _tbl = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavagationBarH, self.width, self.height - kNavagationBarH)];
        _tbl.delegate = self;
        _tbl.dataSource = self;
        [_tbl registerClass:[XMFriendListCell class] forCellReuseIdentifier:@"XMFriendListCellName"];
        if (@available(iOS 15.0, *)) {
            _tbl.sectionHeaderTopPadding = 0;
        } else {
            _tbl.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            // Fallback on earlier versions
        }
    }
    return _tbl;
}

- (NSMutableArray *)friendModelArr {
    if (!_friendModelArr) {
        _friendModelArr = [NSMutableArray array];
    }
    return _friendModelArr;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
